/**
 * 文章评论 article comment
 */
const express = require("express");
const { body, param } = require("express-validator");
const router = express.Router();
const {
  selectCommentList,
  insertComment,
  deleteComment,
  articleCommentLike,
} = require("@root/services/blog/article-comment.service.js");

// 参数校验统一封装
const {
  id,
  article_id,
  content,
  parent_id,
  // reply_id,
  // reply_user_id,
  // reply_nickname,
  // reply_avatar,
} = {
  id: ([prop = "id", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isNumeric({
        no_symbols: true,
      })
      .withMessage(`${prop}必须是正整数`),
  article_id: ([prop = "article_id", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isNumeric({
        no_symbols: true,
      })
      .withMessage(`${prop}必须是正整数`),
  content: ([prop = "content", validator = body] = []) =>
    validator(prop).notEmpty().withMessage(`${prop}不能为空`),
  parent_id: ([prop = "parent_id", validator = body] = []) =>
    validator(prop).notEmpty().withMessage(`${prop}不能为空`),
  reply_id: ([prop = "reply_id", validator = body] = []) =>
    validator(prop).isInt({ no_symbols: true }).withMessage(`${prop}必须是正整数`),
  reply_user_id: ([prop = "reply_user_id", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isInt({ no_symbols: true })
      .withMessage(`${prop}必须是正整数`),
  reply_nickname: ([prop = "reply_nickname", validator = body] = []) =>
    validator(prop).notEmpty().withMessage(`${prop}不能为空`),
  reply_avatar: ([prop = "reply_avatar", validator = body] = []) =>
    validator(prop).notEmpty().withMessage(`${prop}不能为空`),
};

/**
 * @swagger
 * /blog/article/comment/{article_id}:
 *  get:
 *    tags:
 *      - article
 *    description: 获取文章评论列表
 *    operationId: getCommentList
 *    parameters:
 *      - name: article_id
 *        in: path
 *        description: 文章id
 *        required: true
 *        schema:
 *          type: integer
 *    responses:
 *      200:
 *        description: 请求成功返回用户列表
 *        content:
 *          application/json:
 *            schema:
 *              type: array
 *              items:
 *                $ref: '#/components/schemas/ArticleComment'
 *          application/xml:
 *            schema:
 *              type: array
 *              items:
 *                $ref: '#/components/schemas/ArticleComment'
 * components:
 *  schemas:
 *    ArticleComment:
 *      type: object
 *      xml:
 *        name: ArticleComment
 *      properties:
 *        id:
 *          type: integer
 *          format: int64
 *          description: 该条评论id
 *        article_id:
 *          type: integer
 *          description: 文章id
 *        user_id:
 *          type: integer
 *          description: 用户id
 *        nickname:
 *          type: string
 *          description: 用户昵称
 *        avatar:
 *          type: string
 *          description: 用户头像
 *        content:
 *          type: string
 *          description: 评论内容
 *        parent_id:
 *          type: integer
 *          description: 父评论id
 *        reply_id:
 *          type: integer
 *          description: 回复的评论id
 *        reply_user_id:
 *          type: integer
 *          description: 回复的评论用户id
 *        reply_nickname:
 *          type: string
 *          description: 回复的评论用户昵称
 *        reply_avatar:
 *          type: string
 *          description: 回复的评论用户头像
 *        create_time:
 *          type: string
 *          format: date-time
 *          description: 创建时间
 *        children:
 *          type: array
 *          xml:
 *            wrapped: true
 *          items:
 *            type: object
 *            xml:
 *              name: ArticleComment
 *            $ref: '#/components/schemas/ArticleCommentItem'
 *    ArticleCommentItem:
 *      type: object
 *      xml:
 *        name: ArticleCommentItem
 *      properties:
 *        id:
 *          type: integer
 *          format: int64
 *          description: 该条评论id
 *        article_id:
 *          type: integer
 *          description: 文章id
 *        user_id:
 *          type: integer
 *          description: 用户id
 *        nickname:
 *          type: string
 *          description: 用户昵称
 *        avatar:
 *          type: string
 *          description: 用户头像
 *        content:
 *          type: string
 *          description: 评论内容
 *        parent_id:
 *          type: integer
 *          description: 父评论id
 *        reply_id:
 *          type: integer
 *          description: 回复的评论id
 *        reply_user_id:
 *          type: integer
 *          description: 回复的评论用户id
 *        reply_nickname:
 *          type: string
 *          description: 回复的评论用户昵称
 *        reply_avatar:
 *          type: string
 *          description: 回复的评论用户头像
 *        create_time:
 *          type: string
 *          format: date-time
 *          default: "YYYY-MM-DD HH:mm:ss"
 *          description: 创建时间
 */
router.get(
  "/:article_id",
  [param("article_id").isInt().withMessage("文章id必须是正整数")],
  selectCommentList
);

/**
 * @swagger
 * "/blog/article/comment": {
 *    "post": {
        "tags": [
          "article"
        ],
        "summary": "文章评论",
        "description": "添加文章一级评论或评论下的二级评论",
        "operationId": "deleteArticleComment",
        "parameters": [],
        "security": [
          {
              "Authorization": []
          }
        ],
        "requestBody":{
          "description": "添加文章评论参数",
          "content": {
            "application/json": {
              "schema": {
                "required": ["article_id", "content", "parent_id"],
                "type": "object",
                "properties": {
                  "article_id": {
                    "type": "integer",
                    "description": "评论的文章id"
                  },
                  "content": {
                    "type": "string",
                    "description": "评论内容",
                    "example": "评论内容"
                  },
                  "parent_id": {
                    "type": "integer",
                    "description": "评论的父id,如果为0是文章下的一级评论，否则是文章评论下的二级评论"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "请求成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": '#/components/schemas/ArticleComment'
                  }
                }
              }
            }
          }
        }
      }
    }
 */
router.post(
  "/",
  [
    article_id(),
    content(),
    parent_id(),
    // reply_id(),
    // reply_user_id(),
    // reply_nickname(),
    // reply_avatar(),
  ],
  insertComment
);

/**
 * @api {delete} /blog/article/comment/:id 删除文章评论
 */
router.delete("/:id", [id(["id", param])], deleteComment);

/**
 * 文章评论 点赞和取消
 */
router.patch("/like", [article_id(), id(["comment_id"])], articleCommentLike);

module.exports = router;
